********************************************************************************		
********************** Conditional logit: main regressions *********************
********************************************************************************		
		
		use "ZA6804_v6-0-0_processed_stacked_votechoice.dta", clear		
		
	*** Regressions for main table
				* estimate model 5 to hold number of observations constant
					quietly cmclogit vote w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP, /// 
									casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) 
					gen include=1 if e(sample)
	
		* model with only perceived position dummies
			* economic and cultural separately 
			cmclogit vote w7_party_highertaxes_dum w7_party_migrrestr_dum if include==1, /// 
				casevars(ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age)
			
			eststo m1
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
				
			
			* dummy for perception as left-authoritarian
			cmclogit vote w7_party_leftauth if include==1, /// 
				casevars(ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age)		
				
			eststo m2
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
				
			* dummy for all four quadrants
			cmclogit vote w7_party_leftauth w7_party_rightauth w7_party_rightlib w7_party_leftlib if include==1, /// 
				casevars(ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age)

			eststo m3
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
				
		* models with perceived position dummies and salience
			cmclogit vote w7_party_highertaxes_dum w7_party_migrrestr_dum if include==1, /// 
					casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age)	

			eststo m4

				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
			
		* models with interaction between perceived position dummies and salience
			cmclogit vote w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP if include==1, /// 
						casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) 
			
			eststo m5

				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
			
		* different handling of missing values (altwise)		

			cmclogit vote w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP, /// 
						casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) altwise
				
			eststo m6

				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
		
			* Likelihood ratio test
				lrtest m5 m1
				lrtest m5 m4
					// -> m5 fits significantly better than m1 and m4
					
		* write short table for article
			esttab m1 m2 m3 m4 m5 m6 ///
				using Table_condlogit.rtf, b(%8.2f) se(%8.2f) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) varwidth(20) nogaps compress ///
				drop(2.w7_leftright_cat east income_st rural2 christian_d female 1.educat 2.educat 3.educat age 0.w7_mip_cat _cons) ///				
				noomitted label replace noconstant nomtitles

		* write long table with controls included for appendix
			esttab m1 m2 m3 m4 m5 m6 ///
				using Table_condlogit_full.rtf, b(%8.2f) se(%8.2f) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) varwidth(20) nogaps compress ///
				drop(2.w7_leftright_cat 0.w7_mip_cat 1.educat) ///				
				noomitted label replace noconstant nomtitles
		
		
	*** FIGURE 4: Graph conditional probabilities
		* rerun model 6 new with just numeric party variable (due to Stata bug in current version with margins command when using labelled choice variable in conditional logit)
				
				gen obs_party_num=obs_party
				
				cmset personid obs_party_num
				
				cmclogit vote w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP, /// 
						casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) altwise
				eststo m6
				
				
		*** AfD	
				estimates restore m6

				margins, at(w7_party_highertaxes_dum=(0 1) party_highertaxesXeconMIP=(0 1) w7_mip_cat=(0 1 2) ///
						    party_migrrestrXmigrMIP=(0 1) w7_party_migrrestr_dum=(1)) alternative(7) outcome(7) post
				eststo margo1
				
				coefplot ///
						(margo1, keep(1._at)  mcolor(black) ciopts(color(black black)) mlabcolor(black)) /// 
						(margo1, keep(13._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						(margo1, keep(2._at) mcolor(black) ciopts(color(black black)) mlabcolor(black)) ///
						(margo1, keep(20._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						(margo1, keep(6._at) mcolor(black) ciopts(color(black black)) mlabcolor(black)) ///
						(margo1, keep(18._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						, offset(0) legend(off) ///
						coeflabel(1._at="not econ. left, MIP other" 2._at="not econ. left, MIP econ." 6._at="not econ. left, MIP immi." ///
					     13._at="econ. left, MIP other" 18._at="econ. left, MIP immi." 20._at="econ. left, MIP econ.") ///
								 level(95 85) ///
								 mlabel format(%9.3g) mlabposition(12) mlabgap(*2) mlabsize(vsmall) ///
								 title("AfD", size (small)) ///
								 name(predvote_afd, replace) nodraw

								 
		*** The Left	
				estimates restore m6

				margins, at(w7_party_migrrestr_dum=(0 1) party_migrrestrXmigrMIP=(0 1) w7_mip_cat=(0 1 2) ///
							party_highertaxesXeconMIP=(0 1) w7_party_highertaxes_dum=(1)) alternative(6) outcome(6) post
				eststo margo2
				
					coefplot ///
						(margo2, keep(1._at)  mcolor(black) ciopts(color(black black)) mlabcolor(black)) /// 
						(margo2, keep(7._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						(margo2, keep(14._at) mcolor(black) ciopts(color(black black)) mlabcolor(black)) ///
						(margo2, keep(20._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						(margo2, keep(3._at) mcolor(black) ciopts(color(black black)) mlabcolor(black)) ///
						(margo2, keep(12._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						, offset(0) legend(off) ///
						coeflabel(1._at="not restr. immi., MIP other" 14._at="not restr. immi., MIP econ." ///
								3._at="not restr. immi., MIP immi." ///
					     	    7._at="restr. immi., MIP other" 12._at="restr. immi., MIP immi." 20._at="restr. immi., MIP econ.") ///
								 level(95 85) ///
								 mlabel format(%9.3g) mlabposition(12) mlabgap(*2) mlabsize(vsmall) ///
								 title("The Left", size (small)) ///
								 name(predvote_left, replace) nodraw
		
		
		*** CDU/CSU
				estimates restore m6

				margins, at(w7_party_highertaxes_dum=(0 1) party_highertaxesXeconMIP=(0 1) w7_mip_cat=(0 1 2) ///
							party_migrrestrXmigrMIP=(0 1) w7_party_migrrestr_dum=(1)) alternative(1) outcome(1) post
				eststo margo3
				
			
				coefplot ///
						(margo3, keep(1._at)  mcolor(black) ciopts(color(black black)) mlabcolor(black)) /// 
						(margo3, keep(13._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						(margo3, keep(2._at) mcolor(black) ciopts(color(black black)) mlabcolor(black)) ///
						(margo3, keep(20._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						(margo3, keep(6._at) mcolor(black) ciopts(color(black black)) mlabcolor(black)) ///
						(margo3, keep(18._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						, offset(0) legend(off) ///
						coeflabel(1._at="not econ. left, MIP other" 2._at="not econ. left, MIP econ." 6._at="not econ. left, MIP immi." ///
					     13._at="econ. left, MIP other" 18._at="econ. left, MIP immi." 20._at="econ. left, MIP econ.") ///
								 level(95 85) ///
								 mlabel format(%9.3g) mlabposition(12) mlabgap(*2) mlabsize(vsmall) ///
								 title("CDU/CSU", size (small)) ///
								 name(predvote_union, replace) nodraw
			
			
		*** SPD
				estimates restore m6

				margins, at(w7_party_migrrestr_dum=(0 1) party_migrrestrXmigrMIP=(0 1) w7_mip_cat=(0 1 2) ///
							party_highertaxesXeconMIP=(0 1) w7_party_highertaxes_dum=(1)) alternative(3) outcome(3) post
				eststo margo4
				
				coefplot ///
						(margo4, keep(1._at)  mcolor(black) ciopts(color(black black)) mlabcolor(black)) /// 
						(margo4, keep(7._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						(margo4, keep(14._at) mcolor(black) ciopts(color(black black)) mlabcolor(black)) ///
						(margo4, keep(20._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						(margo4, keep(3._at) mcolor(black) ciopts(color(black black)) mlabcolor(black)) ///
						(margo4, keep(12._at) mcolor(gs10) ciopts(color(gs10 gs10)) mlabcolor(gs10)) ///
						, offset(0) legend(off) ///
						coeflabel(1._at="not restr. immi., MIP other" 14._at="not restr. immi., MIP econ." ///
								3._at="not restr. immi., MIP immi." ///
					     	    7._at="restr. immi., MIP other" 12._at="restr. immi., MIP immi." 20._at="restr. immi., MIP econ.") ///
								 level(95 85) ///
								 mlabel format(%9.3g) mlabposition(12) mlabgap(*2) mlabsize(vsmall) ///
								 title("SPD", size (small)) ///
								 name(predvote_spd, replace) nodraw
								
		*** Combine graphs for AfD, The Left, CDU/CSU, and SPD
		graph combine predvote_afd predvote_left predvote_union predvote_spd, xcommon iscale(*1.4) imargin(vsmall) xsize(11) ysize(6) 
	
			
		
																						
********************************************************************************
********************** Conditional logit: Robustness check weights *************
********************************************************************************
								
		* model with only perceived position dummies
			* economic and cultural separately 
			cmclogit vote w7_party_highertaxes_dum w7_party_migrrestr_dum [pweight=wei5_mz] if include==1, /// 
				casevars(ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age)
			
			eststo m7
			
			* dummy for perception as left-authoritarian
			cmclogit vote w7_party_leftauth [pweight=wei5_mz] if include==1, /// 
				casevars(ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age)		
				
			eststo m8
				
			* dummy for all four quadrants
			cmclogit vote w7_party_leftauth w7_party_rightauth w7_party_rightlib w7_party_leftlib [pweight=wei5_mz] if include==1, /// 
				casevars(ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age)

			eststo m9
		
		* models with perceived position dummies and salience
			cmclogit vote w7_party_highertaxes_dum w7_party_migrrestr_dum [pweight=wei5_mz] if include==1, /// 
					casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age)	

			eststo m10

		* models with interaction between perceived position dummies and salience
			cmclogit vote w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP [pweight=wei5_mz] if include==1, /// 
						casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) 
			
			eststo m11
		
		* different handling of missing values (altwise)		
			cmclogit vote w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP [pweight=wei5_mz] if include==1, /// 
						casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) altwise
				
			eststo m12
			
		* write table
			esttab m7 m8 m9 m10 m11 m12 ///
				using Table_condlogit_weights.rtf, b(%8.2f) se(%8.2f) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) varwidth(20) nogaps compress ///
				drop(2.w7_leftright_cat east income_st rural2 christian_d female 1.educat 2.educat 3.educat age 0.w7_mip_cat _cons) ///				
				noomitted label replace noconstant nomtitles


				
********************************************************************************
********************** Conditional logit: robustness regressions ***************
********************************************************************************
	
	*** Table with robustness regressions
		* baseline, altwise
		cmclogit vote w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP, /// 
				casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) altwise

			eststo m1
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
				
		* baseline, altwise, voting intention from wave 7
		cmclogit voteint w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP, /// 
				casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) altwise

			eststo m2	
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
							
		* baseline, altwise, left-right intensity
			cmclogit vote w7_party_highertaxes_dum party_highertaxesXeconMIP w7_party_migrrestr_dum party_migrrestrXmigrMIP, /// 
				casevars(i.w7_mip_cat w7_left_intensity w7_right_intensity east income_st rural2 christian_d female i.educat age) altwise

			eststo m3		
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
							
		* perceived position exact, altwise	
			cmclogit vote w7_party_highertaxes_st w7_party_highertaxes_stXeconMIP w7_party_migrrestr_st w7_party_migrrestr_stXmigrMIP, /// 
				casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) altwise
	
			eststo m4
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
						
		* perceived position exact, altwise, interactions: left right X left-right party perception 	
			cmclogit vote w7_party_highertaxes_st w7_party_highertaxes_stXeconMIP w7_party_migrrestr_st w7_party_migrrestr_stXmigrMIP ///
					 w7_party_lr_st w7_party_lr_stXleft w7_party_lr_stXright, /// 
				casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) altwise
	
			eststo m5
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
							
		* perceived position exact, altwise	
			cmclogit vote w7_party_highertaxes_st w7_party_highertaxes_stXeconMIP w7_party_migrrestr_st w7_party_migrrestr_stXmigrMIP w7_dist_subj_leftright_st, /// 
				casevars(i.w7_mip_cat east income_st rural2 christian_d female i.educat age) altwise

			eststo m6
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
									
		* perceived linear distance, altwise
		cmclogit vote w7_dist_subj_econ_st w7_dist_s_econXeconMIP w7_dist_subj_migration_st w7_dist_s_migrationXmigrMIP, /// 
				casevars(i.w7_mip_cat ib2.w7_leftright_cat east income_st rural2 christian_d female i.educat age) altwise
		
			eststo m7
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
							
		* perceived linear distance with leftright, altwise
		cmclogit vote w7_dist_subj_econ_st w7_dist_s_econXeconMIP w7_dist_subj_migration_st w7_dist_s_migrationXmigrMIP w7_dist_subj_leftright_st, /// 
				casevars(i.w7_mip_cat east rural2 christian_d female i.educat age) altwise
			
			eststo m8
			
				local LL_full=e(ll)
				quietly	cmclogit vote if e(sample), casevars() 
				local LL_null=e(ll)
				display "Pseudo-R² is" 1-`LL_full'/`LL_null'
							
		* write table	
		esttab m1 m2 m3 m4 m5 m6 m7 m8 ///
				using Table_condlogit_robustness.rtf, b(%8.2f) se(%8.2f) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) varwidth(20) nogaps compress ///
				drop(2.w7_leftright_cat east income_st rural2 christian_d female 1.educat 2.educat 3.educat 0.w7_mip_cat age _cons) ///				
				noomitted label replace noconstant nomtitles note("Note: Missing data handled altwise.")